package com.ihr360.commons.lang;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author david.wei
 */
public final class Enums {
    private static Logger logger = LoggerFactory.getLogger(Enums.class);
    private static final Map<String, EnumMeta> METAS = new HashMap<>();
    private final Map<Enums.EnumMeta, List<Enum>> valueMethods = new HashMap<>();

    public Enums() {
    }


    public static Enums.EnumMeta getEnumMeta(String alias) {
        return (Enums.EnumMeta) METAS.get(alias);
    }

    public static Class<Enum> getEnumMetaClass(String alias) {
        Enums.EnumMeta meta = getEnumMeta(alias);
        return null == meta ? null : meta.type;
    }

    public static void addEnumAlias(String className) {
        try {
            Class e = Class.forName(className);
            addEnumAlias(StringUtils.uncapitalize(e.getSimpleName()), e);
        } catch (ClassNotFoundException var2) {
            logger.info(var2.getMessage(), var2);
        }

    }

    public static void addEnumAlias(String alias, Class<Enum> enumClass) {
        Assert.isTrue(!METAS.containsKey(alias), String.format("duplicate enum alias: %s", new Object[]{alias}));
        Enums.EnumMeta meta = new Enums.EnumMeta(enumClass, alias);
        METAS.put(alias, meta);
    }

    private static class EnumMeta {
        private Class<Enum> type;
        private String alias;

        public EnumMeta(Class<Enum> type, String alias) {
            this.type = type;
            this.alias = alias;
        }
    }
}